"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 有事务提交情况下，数据库正常停止
Create At   : 2020/10/13
@zou_jialiang0505328126
Description :
    1、产生持续事务
    2、数据库停止 gs_om -t stop
    3、数据库启动 gs_om -t start
Expect      :
History     :
    Modified by @wan005 2021/1/28: 增加对主节点状态查询，防止主节点处于recovery状态无法连接
"""
import os
import unittest
from yat.test import Node
import time
from yat.test import macro
import sys
import datetime

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.ComThread import ComThread

logger = Logger()
common = Common('PrimaryRoot')
commonshpri = CommonSH('PrimaryDbUser')
commonshsta1 = CommonSH('Standby1DbUser')
commonshsta2 = CommonSH('Standby2DbUser')


class ModifyConnection(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')
    dbStandby1RootNode = Node(node='Standby1Root')
    dbStandby1UserNode = Node(node='Standby1DbUser')
    dbStandby2UserNode = Node(node='Standby2DbUser')
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    Constant = Constant()
    TPCC_PATH = macro.TPCC_PATH

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info("-----------reliability_humanfactor009 start-----------")

    def test_modify_pidfile(self):
        logger.info("-----------start TPCC--------------------")
        tpcc_thread = ComThread(common.startTPCC, args=(self.dbPrimaryRootNode, self.TPCC_PATH))
        tpcc_thread.start()

        time.sleep(30)

        logger.info("-----------stop opengauss--------------------")
        result = commonshpri.stopDbCluster()
        self.assertTrue(result)

        logger.info("-------------start opengauss---------------------")
        msg = commonshpri.startDbCluster(True)
        result = self.Constant.START_SUCCESS_MSG in msg or 'Degraded' in msg
        self.assertTrue(result)
        if 'Degraded' in msg:
            result = commonshpri.wait_cluster_connected()
            self.assertTrue(result)

        logger.info("-------------check tpcc result---------------------")
        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        logger.info(tpcc_result)
        self.assertNotIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

        logger.info("----------------check duty page----------------------")
        result = commonshpri.check_whether_remain_dirty_page()
        self.assertTrue(result)
        logger.info("-------------check data consistency-------------------")
        flag = commonshsta1.check_data_consistency()
        self.assertTrue(flag)
        flag = commonshsta2.check_data_consistency()
        self.assertTrue(flag)

        nodes_tuple = (self.dbPrimaryUserNode, self.dbStandby1UserNode, self.dbStandby2UserNode)
        flag = common.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag)

        table_dict = common.format_sql_result(commonshpri.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = common.check_data_sample_by_all(select_sql, *nodes_tuple)
            self.assertTrue(flag)

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
